package com.sub.util;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Description: TODO(身份证)
 * @author wangkai
 * @date 2019年1月29日
 *
 */
public class IdCradUtil {

	/**
	 * 身份证号验证
	 * 
	 * @param idcrad
	 * @return map{flag,msg}
	 */
	public static Map<String, Object> isValidate(String idcrad) {
		Map<String, Object> result = new HashMap<String, Object>();
		StringBuffer msg = new StringBuffer();
		result.put("flag", false);
		if ((idcrad != null && idcrad.length() == 15) || (idcrad != null && idcrad.length() == 18)) {
			String mach = "^[1-8][0-7][0-9]{15}[0-9,X]$";
			if (idcrad.length() == 15) {
				mach = "^[1-8][0-7][0-9]{13}$";
			}
			idcrad = idcrad.toUpperCase();
			if (Pattern.compile(mach).matcher(idcrad).matches()) {
				if (idcrad.length() == 15) {
					idcrad = transIDCard15to18(idcrad);
				}
				String id17 = idcrad.substring(0, 17);
				String id18 = idcrad.substring(17);
				if (id18.equals(transCardLastNo(id17))) {
					result.put("flag", true);
					msg.append("身份证号有效");
				} else {
					msg.append("身份证号无效");
				}
			} else {
				msg.append("身份证号不满足验证规则");
			}
		} else {
			msg.append("身份证号位数不满足");
		}
		result.put("msg", msg);
		return result;
	}

	/**
	 * 15位身份证转18位
	 * 
	 * @param IdCardNO
	 * @return
	 */
	public static String transIDCard15to18(String IdCardNO) {
		String cardNo = null;
		if (null != IdCardNO && IdCardNO.trim().length() == 15) {
			IdCardNO = IdCardNO.trim();
			StringBuffer sb = new StringBuffer(IdCardNO);
			sb.insert(6, "19");
			sb.append(transCardLastNo(sb.toString()));
			cardNo = sb.toString();
		}
		return cardNo;
	}

	/**
	 * 方法用途：15位补全‘19’位后的身份证号码
	 * 
	 * @param newCardId
	 * @return
	 * @return String
	 */
	private static String transCardLastNo(String newCardId) {
		char[] ch = newCardId.toCharArray();
		int m = 0;
		int[] co = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
		char[] verCode = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
		for (int i = 0; i < newCardId.length(); i++) {
			m += (ch[i] - '0') * co[i];
		}
		int residue = m % 11;
		return String.valueOf(verCode[residue]);
	}

	/**
	 * 出生日期返回年龄
	 * 
	 * @param birthDay
	 * @return
	 * @throws Exception
	 */
	public static int getAge(Date birthDay) throws Exception {
		Calendar cal = Calendar.getInstance();
		if (cal.before(birthDay)) {
			throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
		}
		int yearNow = cal.get(Calendar.YEAR);
		int monthNow = cal.get(Calendar.MONTH);
		int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
		cal.setTime(birthDay);
		int yearBirth = cal.get(Calendar.YEAR);
		int monthBirth = cal.get(Calendar.MONTH);
		int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
		int age = yearNow - yearBirth;
		if (monthNow <= monthBirth) {
			if (monthNow == monthBirth) {
				if (dayOfMonthNow < dayOfMonthBirth)
					age--;
			} else {
				age--;
			}
		}
		return age;
	}

}
